﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.Facturation
{
    /// <summary>
    /// Interaction logic for P_TypeItemFacture.xaml
    /// </summary>
    public partial class P_TypeItemFacture : Page
    {
        HotelClient srvService;
        Type_Item_Facture itemFacture;

        public P_TypeItemFacture()
        {
            InitializeComponent();
        }

        private void gererListeTypeItemFacture(object sender, RoutedEventArgs e)
        {

        }

        private void bt_add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Type_Item_Facture itemF = new Type_Item_Facture();

                itemF.Nom_Type_Item_Facture = tb_nom.Text.Trim();
                itemF.Description_Type_Item_Facture = tb_description.Text.Trim();

                srvService = new HotelClient();
                srvService.Open();
                int resultatAjout = srvService.AddTypeItemFacture(itemF);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("L'element [" + itemF.Nom_Type_Item_Facture + "] est bien ajouté",
                                    "Ajout Item Facture",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeItemFacture();
                }
                else
                {
                    MessageBox.Show("Erreur d'ajout de l'element [" + itemF.Nom_Type_Item_Facture + "]",
                                    "Ajout Item Facture",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Item Facture", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void loadListeItemFacture()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des elements de faturation en cours...");
                    srvService = new HotelClient();
                    srvService.Open();
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_TypeItemFacture.DataContext = srvService.getListItemFacture();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Item Facture", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                busyTool.Width = SystemParameters.WorkArea.Width;
                busyTool.Height = SystemParameters.WorkArea.Height;
                loadListeItemFacture();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
